package com.sunelec.controller;

import com.sunelec.pojo.ComparePower;
import com.sunelec.pojo.Power;
import com.sunelec.service.ComparePowerService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @Author: 郑明利
 * @Description:
 * @Date: Create in 18:10 2018/05/21
 * @Modified By:
 */
@Controller
public class ComparePowerController {

    @Autowired
    private ComparePowerService comparePowerService;

    /**
     * 查询相应电站信息
     */
    @RequestMapping("/comparePower")
    public String comparePower(String powerIds, Model model){
        List<ComparePower> comparePowerList = new ArrayList<>();
        String[] powerArray = powerIds.split(",");
        List<Power> powerList = new ArrayList<>();
        for(int i=0;i<powerArray.length;i++){
            String powerId = powerArray[i];
            Power power = comparePowerService.selectByPrimaryKey(powerId);
            powerList.add(i,power);
        }

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        int day = cal.get(Calendar.DATE);
        String month_date = year + "-" + month + "-" + day;

        for(int i=0;i<powerList.size();i++){
            ComparePower comparePower = new ComparePower();
            String[] powerSNArray = powerList.get(i).getPowerSn().split(",");
            float dailyElec = 0;
            float allElec = 0;
            int[] current_powers = new int[8];
            String[] current_power_times = new String[8];
            for(int j=0;j<powerSNArray.length;j++){
                dailyElec += comparePowerService.selectDailyElec(powerSNArray[j],month_date);
                allElec += comparePowerService.selectAllElec(powerSNArray[j]);
                for (int k=0;k<8;k++){
                    try{
                        if(comparePowerService.selectCurrentPowers(powerSNArray[j]).get(k).getCurrentPower()!=null){
                            current_powers[k] = comparePowerService.selectCurrentPowers(powerSNArray[j]).get(k).getCurrentPower();
                            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
                            String dateString = formatter.format(comparePowerService.selectCurrentPowers(powerSNArray[j]).get(k).getUpdateTime());
                            current_power_times[k] = dateString;
                        }
                    }catch (Exception e){
                        current_powers[k] = 0;
                        current_power_times[k]="-:-:-";
                    }
                }
                for (int start = 0, end = current_powers.length - 1; start < end; start++, end--) {
                    int temp = current_powers[end];
                    current_powers[end] = current_powers[start];
                    current_powers[start] = temp;
                }
                for (int start = 0, end = current_power_times.length - 1; start < end; start++, end--) {
                    String temp = current_power_times[end];
                    current_power_times[end] = current_power_times[start];
                    current_power_times[start] = temp;
                }
            }
            JSONArray current_power = JSONArray.fromObject(current_powers);
            JSONArray current_power_time = JSONArray.fromObject(current_power_times);
            comparePower.setPowerId(powerList.get(i).getPowerId());
            comparePower.setAllElec(allElec);
            comparePower.setDailyElec(dailyElec);
            comparePower.setNowDay(month_date);
            comparePower.setPowerName(powerList.get(i).getPowerName());
            comparePower.setCurrent_power(current_power);
            comparePower.setCurrent_power_time(current_power_time);
            comparePowerList.add(i,comparePower);
        }
        model.addAttribute("comparePowerList",comparePowerList);
        return "dataCompare";
    }
}
